<?php

namespace App\Logic;

use App\common;
use App\Http\Controllers\Controller;
use App\Logic\realeEstateLogic;
use App\Models\Fang;
use App\Models\Resources;
use Illuminate\Http\Request;

/**
 * 房源
 * @name zf
 */
class FangLogic
{
    /**
     * Undocumented function
     * 列表
     * @param [type] $name
     * @param [type] $phone
     * @return void
     */
    public static function fangList()
    {
        return Fang::list();
    }

    /**
     * Undocumented function
     * 房源多条件搜索
     * @param [type] $name
     * @param [type] $phone
     * @return void
     */
    public static function fangSearch($name, $xiaoqu, $shi, $wei, $ting, $paginate)
    {
        if (!empty($name)) {
            $where[] = ['fang_name', 'like', "%$name%"];
        }
        if (!empty($xiaoqu)) {
            $where[] = ['fang_xiaoqu', 'like', "%$xiaoqu%"];
        }
        if (!empty($shi)) {
            $where[] = ['fang_shi' => "$shi"];
        }
        if (!empty($wei)) {
            $where[] = ['fang_wei' => "$wei"];
        }
        if (!empty($ting)) {
            $where[] = ['fang_ting' => "$ting"];
        }
        if (empty($paginate)) {
            $paginate = 10;
        }
        $data = Fang::wheres($where, $paginate);
        return $data;

    }

    /**
     * Undocumented function
     * 详情
     * @param [type] $id
     * @return void
     */
    public static function fangDetail($id)
    {

        return Fang::detail('id', $id);
    }

    /**
     * Undocumented function
     * 编辑信息
     * @param [type] $id
     * @return void
     */
    public static function fangSave($admin)
    {
        $admin['password'] = bcrypt($admin['password']);
        if (empty($admin['id']) || !isset($admin['id'])) {

            $user = Fang::create($admin);
            return $user;
        } else {
            return Fang::where('id', $admin['id'])->update($admin);
        }
    }

    /**
     * Undocumented function
     * 删除
     * @param [type] $id
     * @return void
     */
    public static function fangDel($vid)
    {
        if (!empty($vid)) {
            $id = $vid;
        }
        return Fang::del('id', $id);
    }

    /**
     * 房源列表
     * @yuankai
     */
    public static function list()
    {
        $res = Resources::list();
        return $res;
    }

    /**
     * 房源分类搜索
     * @yuankai
     */
    public static function search($category)
    {
        if (!empty($category)){
            $where[] = ['category','like',$category];
        }
        $res = Resources::search($category);
        if ($res) {
            return $res;
        } else {
            throw new \Exception('没有匹配的房源');
        }
    }

    /**
     * @param $neighborhood
     * @return mixed
     * 附近房源
     * @yuankai
     */
    public static function nearby_search($neighborhood)
    {
        if (!empty($neighborhood)){
            $where[] = ['neighborhood','like',$neighborhood];
        }
        $res = Resources::nearby_search($where);
        return $res;
    }

    /**
     * 价格区间查询
     * @yuankai
     */
    public static function price($price)
    {
        if (!empty($price)){
            $where[] = ['price','<=',"$price"];
        }
        $res = Resources::price($where);
        return $res;
    }

    /**
     * 房源户型查询
     * @yuankai
     */
    public static function house_type($bedrooms,$area,$bathrooms)
    {
        if(!empty($bedrooms)){
            $where[]=['bedrooms','<=',"$bedrooms"];
        }
        if(!empty($area)){
            $where[]=['area','<=',"$area"];
        }
        if(!empty($bathrooms)){
            $where[]=['bathrooms','<=',"$bathrooms"];
        }
        return Resources::house_type($where);
    }

    /**
     *房源多条件搜索
     * @yuankai
     */
    public static function more($category,$rent_type)
    {
        if(!empty($category)){
            $where[]=['category','like',$category];
        }
        if(!empty($rent_type)){
            $where[]=['rent_type','like',$rent_type];
        }
        return Resources::more($where);
    }

    /**
     * @param $address
     * @return mixed
     * 房源经纬度转换
     * @yuankai
     */
    public static function getCity($city)
    {
        $key = config('gao.key');
        $curl = "https://restapi.amap.com/v3/geocode/geo?address=$city&output=JSON&key=$key";
        $content = file_get_contents($curl);
        $result = json_decode($content, true);
        //dd($result);
        $location = $result['geocodes'][0]['location'];
        //将经纬度字符串拆分成经度和纬度
//            $longitude = explode(",",$location)[0];
//            $latitude = explode(",",$location)[1];
        return $location;
    }
}
